# -*- coding:utf-8 -*-
# @FileName :betting_base.py
# @Created  :2024/9/30
# @Author   :天空之城
class BettingBase:
    def __init__(self, small_blind=10, big_blind=20):
        """
        初始化下注管理器对象。
        Betting 类继承自 PokerActions 类，用于管理德州扑克游戏中的下注逻辑。

        属性:
        current_bet: int
            当前轮次的最高下注金额。
        pot: int
            当前底池的总金额。
        side_pots: list
            边池列表，用于处理全押(all-in)情况。
        bets: dict
            记录每个玩家当前的下注金额，键是玩家对象，值是下注金额。
        """
        self.small_blind = small_blind
        self.big_blind = big_blind
        self.dealer_position = 0  # 初始庄家位置
        self.current_bet = 0  # 当前轮次最高下注金额
        self.pot = 0  # 当前底池总金额
        self.side_pots = []  # 边池列表
        self.bets = {}  # 记录每个玩家当前的下注金额

    def call(self, player):
        """
        处理玩家跟注

        :param player: Player对象
        """
        required_call = self.current_bet - self.bets.get(player, 0)
        if required_call > player.chips:
            raise ValueError("玩家筹码不足以跟注")

        player.call(required_call)
        self.bets[player] = self.current_bet
        self.pot += required_call

    def fold(self, player):
        """
        处理玩家弃牌

        :param player: Player对象
        """
        player.fold()
        if player in self.bets:
            del self.bets[player]

    def all_in(self, player):
        """
        处理玩家全押

        :param player: Player对象
        """
        player.all_in()
        self.bets[player] = player.current_bet
        self.current_bet = max(self.current_bet, self.bets[player])
        self.pot += player.current_bet

        # 处理边池逻辑
        self._update_side_pots(player)

    def raise_bet(self, player, amount):
        """
        处理玩家加注

        :param player: Player对象
        :param amount: 加注金额
        """
        if amount <= 0 or amount > player.chips:
            raise ValueError("加注金额无效")

        total_bet = self.current_bet + amount
        player.raise_bet(total_bet - self.bets.get(player, 0))
        self.bets[player] = total_bet
        self.current_bet = total_bet
        self.pot += amount

    def place_bet(self, player, amount):
        """
        处理玩家下注

        :param player: Player对象
        :param amount: 下注金额
        """
        if amount <= 0 or amount > player.chips:
            raise ValueError("下注金额无效")

        player.bet(amount)
        self.bets[player] = self.bets.get(player, 0) + amount
        self.current_bet = max(self.current_bet, self.bets[player])
        self.pot += amount

    def reset(self):
        """
        每轮结束后重置下注状态
        """
        self.current_bet = 0
        self.pot = 0
        self.dealer_position += 1
        self.side_pots = []
        self.bets = {}

    def resolve_bets(self, winner):
        """
        结束当前下注轮次并将筹码分配给赢家。

        :param winner: 赢得当前轮次的玩家
        :return: 返回赢家获得的总筹码数
        """
        print(f'玩家 {winner.name}, 原筹码数量: {winner.chips}')
        winner.chips += self.pot
        print(f"玩家 {winner.name} 赢得 {self.pot} 筹码。玩家 {winner.name} 目前筹码数量 {winner.chips}")
        self.reset()

    def __str__(self):
        return f"Betting(current_bet={self.current_bet}, pot={self.pot}, side_pots={self.side_pots}, bets={self.bets})"
